package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import function.FunctionHandler;






/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class TextEditor extends JFrame {
	private JMenuBar menu_bar_main;
	private JMenuItem item_toggle;
	private JMenu menu_view;
	private JMenuItem item_save;
	private JMenuItem open_item;
	private JButton button_toolbar_open;
	private JMenuItem paste_item;
	private JMenuItem item_undo;
	private JButton button_background_color;
	private JButton button_text_color;
	private JButton button_paste;
	private JButton button_copy;
	private JButton button_cut;
	private JMenuItem item_txtc;
	private JMenuItem item_bgc;
	private JMenu menu_color;
	private JButton button_toolbar_save;
	private JMenuItem item_find;
	private JMenuItem copy_item;
	private JMenuItem cut_item;
	private JEditorPane text_input_pane;

	private JButton button_toolbar_new;
	private JPanel toolbar;
	private JMenuItem exit_item;
	private JMenuItem new_item;
	private JMenu edit_menu;
	private JMenu file_menu;
	private JScrollPane text_scroll_pane;

	private eventhandler handle = new eventhandler();
	FunctionHandler handler = new FunctionHandler(this);

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				TextEditor inst = new TextEditor();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public TextEditor() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setLayout(null);

			this.setResizable(false);
			this.setSize(800, 600);

			toolbar = new JPanel();
			getContentPane().add(toolbar, BorderLayout.CENTER);
			toolbar.setBackground(new Color(0,0,0));
			toolbar.setBounds(0, 0, 792, 50);
			toolbar.setLayout(null);

			//toolbar.setSize(new Dimension(792,50));
			text_input_pane = new JEditorPane();
			text_scroll_pane = new JScrollPane();
			getContentPane().add(text_scroll_pane);
			//text_input_pane.setBounds(0, 51, 792, 498);
			text_scroll_pane.setBounds(0, 51, 792, 498);


			getContentPane().add(text_scroll_pane);
			text_scroll_pane.setViewportView(text_input_pane);

			//text_scroll_pane.setVisible(true);
			//text_input_pane.setVisible(true);


			button_toolbar_new = new JButton();
			toolbar.add(button_toolbar_new);
			button_toolbar_new.setText("New");
			button_toolbar_new.setBounds(0, 0, 65, 50);
			button_toolbar_new.setToolTipText("New File");
			button_toolbar_new.addActionListener(handle);
			button_toolbar_new.setActionCommand("new");




			button_toolbar_open = new JButton();
			toolbar.add(button_toolbar_open);
			button_toolbar_open.setText("Open");
			button_toolbar_open.setBounds(66, 0, 65, 50);
			button_toolbar_open.setToolTipText("Open File");
			button_toolbar_open.addActionListener(handle);
			button_toolbar_open.setActionCommand("open");

			button_toolbar_save = new JButton();
			toolbar.add(button_toolbar_save);
			button_toolbar_save.setText("Save");
			button_toolbar_save.setBounds(132, 0, 65, 50);
			button_toolbar_save.setToolTipText("Save File");
			button_toolbar_save.addActionListener(handle);
			button_toolbar_save.setActionCommand("save");

			button_cut = new JButton();
			toolbar.add(button_cut);
			button_cut.setText("Cut");
			button_cut.setBounds(198, 0, 65, 50);
			button_cut.setToolTipText("Cut");
			button_cut.addActionListener(handle);
			button_cut.setActionCommand("cut");

			button_copy = new JButton();
			toolbar.add(button_copy);
			button_copy.setText("Copy");
			button_copy.setBounds(264, 0, 65, 50);
			button_copy.setToolTipText("Copy");
			button_copy.addActionListener(handle);
			button_copy.setActionCommand("copy");

			button_paste = new JButton();
			toolbar.add(button_paste);
			button_paste.setText("Paste");
			button_paste.setBounds(330, 0, 65, 50);
			button_paste.setToolTipText("Paste");
			button_paste.addActionListener(handle);
			button_paste.setActionCommand("paste");

			button_text_color = new JButton();
			toolbar.add(button_text_color);
			button_text_color.setText("Text");
			button_text_color.setBounds(396, 0, 65, 50);
			button_text_color.setToolTipText("Set Text Color");
			button_text_color.addActionListener(handle);
			button_text_color.setActionCommand("text");

			button_background_color = new JButton();
			toolbar.add(button_background_color);
			button_background_color.setText("BackGround");
			button_background_color.setBounds(462, 0, 110, 50);
			button_background_color.setToolTipText("Set Background Color");
			button_background_color.addActionListener(handle);
			button_background_color.setActionCommand("background");

			menu_bar_main = new JMenuBar();
			setJMenuBar(menu_bar_main);

			file_menu = new JMenu();
			menu_bar_main.add(file_menu);
			file_menu.setText("File");


			new_item = new JMenuItem();
			file_menu.add(new_item);
			new_item.setText("New");
			new_item.addActionListener(handle);
			new_item.setActionCommand("new");

			open_item = new JMenuItem();
			file_menu.add(open_item);
			open_item.setText("Open");
			open_item.addActionListener(handle);
			open_item.setActionCommand("open");

			item_save = new JMenuItem();
			file_menu.add(item_save);
			item_save.setText("Save");
			item_save.addActionListener(handle);
			item_save.setActionCommand("save");

			exit_item = new JMenuItem();
			file_menu.add(exit_item);
			exit_item.setText("Exit");
			exit_item.addActionListener(handle);
			exit_item.setActionCommand("exit");


			edit_menu = new JMenu();
			menu_bar_main.add(edit_menu);
			edit_menu.setText("Edit");

			item_undo = new JMenuItem();
			edit_menu.add(item_undo);
			item_undo.setText("Undo");
			item_undo.addActionListener(handle);
			item_undo.setActionCommand("undo");


			menu_color = new JMenu();
			menu_bar_main.add(menu_color);
			menu_color.setText("Color");

			item_txtc = new JMenuItem();
			menu_color.add(item_txtc);
			item_txtc.setText("Text Color");
			item_txtc.addActionListener(handle);
			item_txtc.setActionCommand("text");

			item_bgc = new JMenuItem();
			menu_color.add(item_bgc);
			item_bgc.setText("BackGround Color");
			item_bgc.addActionListener(handle);
			item_bgc.setActionCommand("background");

			cut_item = new JMenuItem();
			edit_menu.add(cut_item);
			cut_item.setText("Cut");
			cut_item.addActionListener(handle);
			cut_item.setActionCommand("cut");


			copy_item = new JMenuItem();
			edit_menu.add(copy_item);
			copy_item.setText("Copy");
			copy_item.addActionListener(handle);
			copy_item.setActionCommand("copy");


			paste_item = new JMenuItem();
			edit_menu.add(paste_item);
			paste_item.setText("Paste");
			paste_item.addActionListener(handle);
			paste_item.setActionCommand("paste");

			item_find = new JMenuItem();
			edit_menu.add(item_find);
			item_find.setText("Find");
			item_find.setVisible(false);


			menu_view = new JMenu();
			menu_bar_main.add(menu_view);
			menu_view.setText("View");

			item_toggle = new JMenuItem();
			menu_view.add(item_toggle);
			item_toggle.setText("Toggle Toolbar");
			item_toggle.addActionListener(handle);
			item_toggle.setActionCommand("toggle");

			pack();
			this.setSize(800, 600);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



	public JEditorPane getTextPane(){
		return text_input_pane;
	}

	public JPanel getToolBar(){
		return toolbar;
	}

	public JScrollPane getScrollPane() {	
		return text_scroll_pane;
	}



	private class eventhandler implements ActionListener{


		@Override
		public void actionPerformed(ActionEvent e) {

			String cmd =e.getActionCommand();
			System.err.println("HI U:"+ cmd);
			handler.handleAction(cmd);
		}



	}





}
